package net.vonkannen.callduration;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Lay_CallDuration extends Activity implements OnCheckedChangeListener {
	private static final String PREFS_NAME = "CallDuration_prefs";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* Get preferences */
        SharedPreferences settings = getSharedPreferences("CallDuration_prefs", 0);
        boolean PhoneServiceReceiver = settings.getBoolean("PhoneServiceReceiver", true);
        
        /* Get inputs */
    	CompoundButton btn = (ToggleButton) findViewById(R.id.tgl_status);
        btn.setOnCheckedChangeListener(this);
        btn.setChecked(PhoneServiceReceiver);
        
        
        /* Create the adView */
        AdView adView = new AdView(this, AdSize.BANNER, "a14dbc9a647203e");
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.lay_main);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
        layout.addView(adView, lp);
        adView.loadAd(new AdRequest());
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_info:
        	Intent intent_Info = new Intent(this, Lay_Info.class);
        	startActivity(intent_Info);
            return true;
        case R.id.menu_settings:
        	Intent intent_Settings = new Intent(this, Lay_Settings.class);
        	startActivity(intent_Settings);        	
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton btn, boolean isChecked) {
    	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    	SharedPreferences.Editor editor = settings.edit();
    	if(btn.isChecked()) {
    		editor.putBoolean("PhoneServiceReceiver", true);
    	}
    	else {
    		editor.putBoolean("PhoneServiceReceiver", false);
    	}
    	editor.commit();
    }
}